

package com.hazelcast.executor.impl;

import com.hazelcast.core.ExecutionCallback;

import java.util.function.BiConsumer;

/**
 * Adapts an {@link com.hazelcast.core.ExecutionCallback} as a {@link java.util.function.BiConsumer}
 * so it can be used with {@link java.util.concurrent.CompletableFuture#whenComplete(BiConsumer)}.
 */
public class ExecutionCallbackAdapter<T> implements BiConsumer<T, Throwable> {

    private final ExecutionCallback<T> executionCallback;

    public ExecutionCallbackAdapter(ExecutionCallback<T> executionCallback) {
        this.executionCallback = executionCallback;
    }

    @Override
    public final void accept(T response, Throwable t) {
        if (t == null) {
            executionCallback.onResponse((T) interceptResponse(response));
        } else {
            executionCallback.onFailure(t);
        }
    }

    protected Object interceptResponse(Object o) {
        return o;
    }
}
